class QuoteRequestsController < ApplicationController
  before_filter :login_required, :authorization_required, :find_customer
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :put, :only => [ :update ], :redirect_to => { :action => :index }
  verify :method => :post, :only => [ :create ], :redirect_to => { :action => :index }
  verify :method => :delete, :only => [ :destroy ], :redirect_to => { :action => :index }
  
  def index
    if !@customer.nil?
      @quote_requests = @customer.quote_requests  
    else
      @quotes_requests = QuoteRequest.find(:all, :order => 'created_on DESC')
    end
    respond_to do |format|
      format.html {render :action => 'list.rhtml'}
      format.js {render :action => 'list.rjs'}
      format.xml {render :xml => @quote_requests.to_xml}
    end
  end
  
  def show
    @quote_request = QuoteRequest.find(params[:id])
  end
  
  def new
    @quote_request = QuoteRequest.new
    respond_to do |format|
      format.html {render :action => 'new.rhtml'}
      format.js {render :action => 'new.rjs'}
    end
  end
  
  def create
    quote_request = QuoteRequest.new(params[:quote_request])
    @customer.quote_requests << quote_request
    if @customer.save
      respond_to do |format|
        format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
        format.js {render :action => 'list.rjs'}
      end
    else 
      render :action => 'new' unless request.xhr?
    end
  end
  
  def edit
    @quote_request = QuoteRequest.find(params[:id])
    @customer = @quote_request.customer
    respond_to do |format|
      format.html {render :action => 'edit.rhtml'}
      format.js {render :action => 'edit.rjs'}
    end
  end
  
  def update
    @quote_request = QuoteRequest.find(params[:id])
    @customer = @quote_request.customer
    if @quote_request.update_attributes(params[:quote_request])
      respond_to do |format|
        format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
        format.js {render :action => 'list.rjs'}
      end
    else
      render :action => 'edit' unless request.xhr?
    end
  end
  
  def destroy
    QuoteRequest.find(params[:id]).destroy
    index
  end
  
  def destroy_selected
    unless params[:selected].nil?
      params[:selected].each do |id|
        quoteRequest = QuoteRequest.find(id)
        @customer = quoteRequest.customer
        quoteRequest.destroy
      end
    end
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'list.rjs'}
    end
  end
  
  def new_document
    @quote_request = QuoteRequest.find(params[:id])
    @customer = @quote_request.customer
    respond_to do |format|
      format.html {render :action => "new_document.rhtml"}
      format.js {render :action => "new_document.rjs"}
    end
    
  end
  
  def create_document
    @quote_request = QuoteRequest.find(params[:id])
    @customer = @quote_request.customer
    document = Document.new(params[:document])
    @quote_request.documents << document
    if @quote_request.save
      responds_to_parent do
        render :action => 'list.rjs'
      end
    else
      render :action => "new_document" unless request.xhr?
    end
  end
  
  def remove_document
    @quote_request = QuoteRequest.find(params[:id])
    @customer = @quote_request.customer
    document = Document.find(params[:document])
    @quote_request.documents.delete(document)
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'list.rjs'}
    end
  end
end
